package jp.ykmjuku.android.mediacontroler;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;

public class MainActivity extends Activity implements MediaPlayerControl, OnPreparedListener, OnBufferingUpdateListener {
    final String TAG = "MediaControllerSample";
    MediaController mMediaController1;
    MediaPlayer mMediaPlayer;
    Handler mHandler = new Handler();
    int mPercent;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        mMediaController1 = new MediaController(this);
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnBufferingUpdateListener(this);
        try {
            setMedia("http://www.jiraud.com/mp3/superjbfinger.mp3");
        } catch (IllegalArgumentException e) {
            Log.e(TAG, "引数エラー", e);
        } catch (SecurityException e) {
            Log.e(TAG, "セキュリティーエラー", e);
        } catch (IllegalStateException e) {
            Log.e(TAG, "MediaPlayerのステートエラー", e);
        } catch (IOException e) {
            Log.e(TAG, "IOエラー", e);
        }
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
      //the MediaController will hide after 3 seconds - tap the screen to make it appear again
        mMediaController1.show();
      return false;
    }
    
    public void setMedia(String path) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
        mMediaPlayer.setDataSource(path);
        mMediaPlayer.prepare();
        mMediaPlayer.start();
    }

    public boolean canPause() {
        return mMediaPlayer.isPlaying();
    }

    public boolean canSeekBackward() {
        return false;
    }

    public boolean canSeekForward() {
        return false;
    }

    public int getBufferPercentage() {
        return mPercent;
    }

    public int getCurrentPosition() {
        return mMediaPlayer.getCurrentPosition();
    }

    public int getDuration() {
        return mMediaPlayer.getDuration();
    }

    public boolean isPlaying() {
        return mMediaPlayer.isPlaying();
    }

    public void pause() {
        mMediaPlayer.pause();
    }

    public void seekTo(int msec) {
        mMediaPlayer.seekTo(msec);
    }

    public void start() {
        mMediaPlayer.start();
    }

    public void onPrepared(MediaPlayer mp) {
        mMediaController1.setMediaPlayer(this);
        mMediaController1.setAnchorView(findViewById(R.id.main_audio_view));
        mHandler.post(new Runnable() {
            public void run() {
                mMediaController1.setEnabled(true);
                mMediaController1.show();
            }
          });
    }

    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        mPercent = percent;
    }
}